/*
 * mox_master_xc.xc
 *
 *  Created on: Jul 11, 2010
 *      Author: jmonk
 */

#include "mox.h"

void mox_service_slaves(unsigned state, streaming chanend chan_mox[NUM_CORES], unsigned frame)
{
	int i;
	int tmp;
	timer tmr;
	unsigned t;
	unsigned flag = 0;

	for (i = 0; i < NUM_CORES; i++) {
		mox_outct(chan_mox[i], MOX_UPDATE);
		//mox_chan_out_wd(chan_mox[i], frame);
	}

	for (i = 0; i < NUM_CORES; i++) {
		select {
		case chan_mox[i] :> tmp:
			if ((tmp != MOX_ACK) && (tmp != MOX_NACK)) {
				mox_slave_service(state, i);
			}
			flag |= (1 << i);
			tmp = 1;
			break;
		default:
			break;
		}
	}
	tmr :> t;
	while (tmp) {
		select {
		case tmr when timerafter(t+SLAVE_WAIT_DELAY) :> void:
			tmp = 0;
			break;
		default:
			for (i = 0; i < NUM_CORES; i++) {
				select {
				case chan_mox[i] :> tmp:
					if ((tmp != MOX_ACK) && (tmp != MOX_NACK)) {
						mox_slave_service(state, i);
					}
					flag |= (1 << i);
					tmp = 1;
					break;
				default:
					break;
				}
			}
			break;
		}
	}

	for (i = 0; i < NUM_CORES; i++) {
		if (!(flag & (1 << i))) {
			mox_slave_no_respond(state, i);
		}
	}

	return;
}
